let reg = /^(\d{4})\d+(\d{4})$/ // 银行卡 只显示前四位后四位 中间用*代替
let inputNumber = /^[0-9]*$/ // /[^\d]/g      // 只能输入数字
let inputLetter = /[^a-zA-Z]/g // 只能输入字母
let inputNumberLetter = /[\W]/g // 只能输入数字和字母
let inputIdCard = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
let inputBankCard = /^([1-9]{1})(\d{15}|\d{18})$/
let inputVin = /^[\da-zA-Z]{7,8}$/
let phone = /^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/;
let inputAccount = /^[0-9]{12,21}$/
let inputMoney = /^[0-9]{12,21}$/
let name = /^[\u4e00-\u9fa5]{2,}$/ //公司名称
let globalNo = /^[A-Z0-9]{8,}$/ //公司证件号码
let address = /^[\u4e00-\u9fa5]{2,}$/ //公司注册地址

let username = /\S/
let password = /^[0-9a-zA-Z](6-16)$/

//验证数字
export const isNumber = str => {
	return inputNumber.test(str)
}
//验证名称不能空
export const account = str => {
	return username.test(str)
}

//验证手机号
export const mobile = str => {
	return phone.test(str)
}

//验证密码
export const userPassword = str => {
	return password.test(str)
}

//验证身份证
export const userIdCard = str => {
	return inputIdCard.test(str)
}

//图片域名
export const setImgUrl = str => {
	if (!str) {
		return '';
	}
	const reg = /^http.*/
	return reg.test(str) ? str : 'https://www.toshuai.com' + str;
}

//时间戳转换为yyyy-MM-dd hh:mm:ss
//timestamp(秒为单位)时间戳
export const timestampFormat = timestamp => {
	let date = new Date(timestamp * 1000);
	let Y = date.getFullYear() + "-";
	let M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "-";
	let D =
		(date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
	let h =
		(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";

	let m =
		(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
		":";
	let s =
		date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	return Y + M + D + h + m + s;
}

//date指的是new Date(),fmt是格式化的格式
export const fmtFormat = (date = '', fmt) => {
	if (date) {
		date = new Date(date);
	} else {
		date = new Date();
	}
	let o = {
		"M+": date.getMonth() + 1, //月份 
		"d+": date.getDate(), //日 
		"h+": date.getHours(), //小时 
		"m+": date.getMinutes(), //分 
		"s+": date.getSeconds(), //秒 
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度 
		"S": date.getMilliseconds() //毫秒 
	};
	//(y+)匹配多个y，比如yyyy
	if (/(y+)/.test(fmt)) {
		// RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (let k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k])
				.length)));
		}
	}
	return fmt;
}

//获取服富文本
export const getText = str => {
	return str.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, "");
}

export const lastweek = (isToday = 0, index = 0) => {
	let now = new Date(new Date().setDate(new Date().getDate() +
		index)); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
	let year = now.getFullYear(); //getYear()+1900=getFullYear()
	let month = now.getMonth() + 1; //0-11表示1-12月
	let day = now.getDate(); // 当天日期
	let week = now.getDay(); // 当前周

	let weekArr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

	// let dateTime = new Date().getFullYear(); /* 获取现在的年份 */
	if (parseInt(month) < 10) {
		month = '0' + month;
	}
	if (parseInt(day) < 10) {
		day = '0' + day;
	}

	let date = year + '-' + month + '-' + day;
	let week_txt = isToday ? '今天' : weekArr[week];
	let date_txt = month + '-' + day + ' ';
	return {
		date_txt: date,
		week_txt: date_txt + ' ' + week_txt
	}
}

/**
 * 比较时分
 * @param {startTime} startTime 开始的时分秒
 * @param {endTime} endTime 结束的时分秒
 */
export const compareH = (startTime, endTime) => {
	let start = time_to_sec(startTime)
	let end = time_to_sec(endTime, 1)
	if (start > end) {
		return true
	} else {
		return false
	}
}

/**
 * 将时分秒转化为时间戳
 * @param {time} time 时分秒
 * @returns 时分秒的时间戳
 */
export const time_to_sec = (time, isEnd = 0) => {
	if (time) {
		let s = '';
		let hmArr = time.split(':');
		let hour = hmArr[0];
		let min = hmArr[1];
		// let sec = time.split(':')[0];
		let ss = isEnd == 1 ? 60 * 30 : 0;
		s = Number(hour * 3600) + Number(min * 60) + ss;

		return s;
	}
}

//获取前一年或后一年
export const lastYear = (type, index) => {
	let now = new Date(); // 可以传值调式 now = new Date(2019,2,30); 今天是3月30号
	let year = now.getFullYear(); //getYear()+1900=getFullYear()
	let month = now.getMonth() + 1; //0-11表示1-12月
	let day = now.getDate(); // 当天日期
	// let dateTime = new Date().getFullYear(); /* 获取现在的年份 */
	if (parseInt(month) < 10) {
		month = '0' + month;
	}
	if (parseInt(day) < 10) {
		day = '0' + day;
	}
	if (type === 'left') {
		/* 前一年 */
		year = new Date(new Date().setFullYear(year - index)).getFullYear()
	}

	if (type === 'right') {
		/* 后一年 */
		year = new Date(new Date().setFullYear(year + index)).getFullYear()
	}

	return year + '-' + month + '-' + day
}

//获取年龄
export const jsGetAge = strBirthday => {
	let returnAge;
	// 根据生日计算年龄
	//以下五行是为了获取出生年月日，如果是从身份证上获取需要稍微改变一下
	let strBirthdayArr = strBirthday.split("-");
	let birthYear = strBirthdayArr[0];
	let birthMonth = strBirthdayArr[1];
	let birthDay = strBirthdayArr[2];
	let d = new Date();
	let nowYear = d.getFullYear();
	let nowMonth = d.getMonth() + 1;
	let nowDay = d.getDate();
	if (nowYear == birthYear) {
		returnAge = 0; //同年 则为0岁
	} else {
		let ageDiff = nowYear - birthYear; //年之差
		if (ageDiff > 0) {
			if (nowMonth == birthMonth) {
				let dayDiff = nowDay - birthDay; //日之差
				if (dayDiff < 0) {
					returnAge = ageDiff - 1;
				} else {
					returnAge = ageDiff;
				}
			} else {
				let monthDiff = nowMonth - birthMonth; //月之差
				if (monthDiff < 0) {
					returnAge = ageDiff - 1;
				} else {
					returnAge = ageDiff;
				}
			}
		} else {
			returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
		}
	}
	return returnAge; //返回周岁年龄
}

/**
 * 获取图片主题色
 * @param path
 * 图片的路径，可以是相对路径，临时文件路径，存储文件路径，网络图片路径
 * @param canvasId
 * 画布表示
 * @param callback
 * 回调函数，返回图片主题色的 RGB 颜色值
 */
export const getImageThemeColor = (path, canvasId, callback) => {
	uni.getImageInfo({
		src: path,
		success: function(img) {
			// 创建一个 Canvas 对象
			const ctx = uni.createCanvasContext(canvasId);
			console.log(ctx, 'ctx')
			// 将图片绘制到 Canvas 上
			const imgWidth = 300;
			const imgHeight = 150;
			ctx.drawImage(img.path, 0, 0, imgWidth, imgHeight);
			ctx.save();
			setTimeout(() => {

				ctx.draw(true, () => {
					uni.canvasGetImageData({
						canvasId: canvasId,
						x: 0,
						y: 0,
						width: imgWidth,
						height: imgHeight,
						success(res) {
							console.log(res, 'res')
							let data = res.data;
							console.log(data, 'data')
							let arr = [];
							let r = 1,
								g = 1,
								b = 1;
							// console.log(data[((imgWidth * 0) + 0)] +1,'333')
							// 取所有像素的平均值
							for (let row = 0; row < imgHeight; row++) {
								for (let col = 0; col < imgWidth; col++) {
									if (row == 0) {
										r += data[((imgWidth * row) + col)];
										// r += data[imgWidth * row + col];
										g += data[imgWidth * row + col + 1];
										b += data[imgWidth * row + col + 2];
										arr.push([r, g, b]);
									} else {
										r += data[(imgWidth * row + col) * 4];
										g += data[(imgWidth * row + col) * 4 +
											1];
										b += data[(imgWidth * row + col) * 4 +
											2];
										arr.push([r, g, b]);
									}
								}
							}
							// 求取平均值

							r /= imgWidth * imgHeight;
							g /= imgWidth * imgHeight;
							b /= imgWidth * imgHeight;
							// 将最终的值取整
							r = Math.round(r);
							g = Math.round(g);
							b = Math.round(b);

							if (!!callback) {
								// 返回图片主题色的 RGB 颜色值
								callback(`${r},${g},${b}`);
							}
						},
					});
				});

			}, 1500);
		},
	});
}